/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */     
#include "gb_if.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
osThreadId_t gb_taskHandle;
/* USER CODE END Variables */
osThreadId_t initial_taskHandle;
osThreadId_t system_taskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void gb(void *argument);  
/* USER CODE END FunctionPrototypes */

void initial(void *argument);
void system(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
       
  /* USER CODE END Init */
osKernelInitialize();

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of initial_task */
  const osThreadAttr_t initial_task_attributes = {
    .name = "initial_task",
    .priority = (osPriority_t) osPriorityNormal,
    .stack_size = 128
  };
  initial_taskHandle = osThreadNew(initial, NULL, &initial_task_attributes);

  /* definition and creation of system_task */
  const osThreadAttr_t system_task_attributes = {
    .name = "system_task",
    .priority = (osPriority_t) osPriorityNormal,
    .stack_size = 128
  };
  system_taskHandle = osThreadNew(system, NULL, &system_task_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
	/* definition and creation of gb_task */
  const osThreadAttr_t gb_task_attributes = {
    .name = "gb_task",
    .priority = (osPriority_t) osPriorityRealtime,
    .stack_size = 512
  };
  gb_taskHandle = osThreadNew(gb, NULL, &gb_task_attributes);
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_initial */
/**
  * @brief  Function implementing the initial_task thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_initial */
void initial(void *argument)
{
    
    
    

  /* USER CODE BEGIN initial */
  /* Infinite loop */
  for(;;)
  {
    osDelay(10);
  }
  /* USER CODE END initial */
}

/* USER CODE BEGIN Header_system */
/**
* @brief Function implementing the system_task thread.
				 TO simply detect system stablility;
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_system */
void system(void *argument)
{
  /* USER CODE BEGIN system */
	FSM_STATUS current_status;
	HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
	HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
	static uint8_t led1_dir = 0,led1_count = 0;
	static uint8_t led0_dir = 0,led0_count = 0;
  /* Infinite loop */
  for(;;)
  {
		current_status = get_fsm_status();
		switch(current_status)
		{
			case(FSM_INITIAL):
			{
				HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
				if(!(led1_count % 10) && led1_dir<10)
				{
					HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
					led1_dir ++;
				}				
				if(led1_count>= 99)
				{
					led1_count = 0;
					led1_dir = 0;
					HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
				}
				else
					led1_count++;
				break;
			}
			case(FSM_ERROR):
			{
				HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
				if(!(led0_count % 50) && led0_dir<1)
				{
					HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
					led0_dir ++;
				}				
				if(led0_count>= 99)
				{
					led0_count = 0;
					led0_dir = 0;
				}
				else
					led0_count++;
				
				break;
			}
			default:
			{
				HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
				if(!(led1_count % 5) && led1_dir<4)
				{
					HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
					led1_dir ++;
				}				
				if(led1_count>= 99)
				{
					led1_count = 0;
					led1_dir = 0;
					HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
				}
				else
					led1_count++;
				
				break;
			}
		}

    osDelay(10);
  }
  /* USER CODE END system */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE BEGIN Header_gb */
/**
* @brief Function implementing the gb_task thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_gb */
__weak void gb(void *argument)
{
  /* USER CODE BEGIN gb */
	/*
	
	** function redefine in gb.c --cdy 2019.9.5
	
	*/
  /* USER CODE END gb */
}
/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
